import { getDeviceType, getComacList } from '../../../api/getData.js'
import { formatTime } from '../../../utils/util.js'
const app = getApp()
Component({
  properties: {
    deviceid: {
      type: String
    }
  },
  data: {
    date: '',
    dateTime: '',
    array: [],
    list: [],
    play: null,
    product: null,
    index: 0,
    orderType: -1,
    orderScore: -1,
  },
  attached() {
    let date = formatTime(new Date(), 1, true)
    const dateTime = new Date(date.split('-').join('/') + ' ' + '00:00:00').getTime();
    this.setData({ date, dateTime })
    this.getList(date, this.properties.deviceid)
    this.getDeviceTypefn(this.properties.deviceid);
  },
  pageLifetimes: {
    show() {
      
    }
  },
  methods: {
    getListByNav(id) {
      const { orderType, orderScore, array, index, date } = this.data;
      this.getList(date, id, array[index], orderType, orderScore)
    },
    sortFnName() {
      const { orderType, orderScore, array, index, date } = this.data;
      this.setData({
        orderType: orderType === -1 ? 1 : (orderType === 1 ? 0 : -1),
        orderScore: -1
      })
      this.getList(date, this.properties.deviceid, array[index], this.data.orderType, this.data.orderScore)
    },
    sortFnComp() {
      const { orderType, orderScore, array, index, date } = this.data;
      this.setData({
        orderScore: orderScore === -1 ? 1 : (orderScore === 1 ? 0 : -1),
        orderType: -1
      })
      this.getList(date, this.properties.deviceid, array[index], this.data.orderType, this.data.orderScore)
    },
    bindDateChange(e) {
      let value = e.detail.value;
      const { array, index } = this.data;
      console.log('value', value)
      this.setData({
        date: value
      })

      this.getList(value, this.properties.deviceid, array[index])
    },

    bindPickerChange: function (e) {
      const { array, dateTime } = this.data;
      this.setData({
        index: e.detail.value
      })
      this.getList(dateTime, this.properties.deviceid, array[e.detail.value])
    },
    // 前一天后一天
    changeDay(e) {
      const { array, index } = this.data;
      let today = this.data.date;
      let sign = e.currentTarget.dataset.sign;
      let stm = new Date(today.split('-').join('/')).getTime();
      let newDay = stm + sign * 86400000
      let date = formatTime(new Date(newDay), 1, true)
      let postData = formatTime(new Date(newDay), 0, true)
      this.setData({ date })
      this.getList(new Date(postData).getTime(), this.properties.deviceid, array[index])
    },

    // 获取列表
    getList(time, deviceId, partType, orderType, orderScore) {
      let _time =""+time;
      if(_time.indexOf('-') > -1 ){
        _time=new Date(time.split('-').join('/') + ' ' + '00:00:00').getTime();
      }

      getComacList(_time, deviceId, partType, orderType, orderScore)
        .then(res => {
          if (res.resCode === 'SUCCESS') {
            this.setData({ list: res.result.list })
          }
        })
    },

    getDeviceTypefn(data) {
      getDeviceType(data).then((res) => {
        if (res.resCode === 'SUCCESS') {
          let _array=['all'].concat(res.result)
          this.setData({
            array: _array,
          })
        }
      })
    },
  }
})